package com.aps.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.aps.entity.ApsExeption;
import com.aps.entity.domain.dish.DishSpec;
import com.aps.entity.eunm.ResultType;
import com.aps.page.BaseQuery;
import com.aps.page.Pagination;
import com.aps.service.IDishSpecService;
import com.aps.service.rep.DishSpecRep;

/**
 * @author liaoguangan
 * @description <>
 * @date 2018/4/9 14:02
 */

@Service
public class DishSpecServiceImpl implements IDishSpecService {

    @Autowired
    private DishSpecRep dishSpecRep;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 增加
     *
     * @param dishSpec
     * @return
     */
    @Override
    public DishSpec add(DishSpec dishSpec) {
        DishSpec ds = dishSpecRep.findByCode(dishSpec.getCode());
        if (null != ds) {
            throw new ApsExeption(ResultType.RECORD_ALWAYS_EXIST);
        }
        return dishSpecRep.save(dishSpec);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void remove(String id) {
        DishSpec ds = dishSpecRep.findOne(id);
        if (null == ds) {
            throw new ApsExeption(ResultType.RECORD_NOT_EXIST);
        }
        ds.setIsDeleted(true);
        dishSpecRep.save(ds);
    }

    /**
     * 更新
     *
     * @param dishSpec
     * @return
     */
    @Override
    public DishSpec update(DishSpec dishSpec) {
        DishSpec ds = dishSpecRep.findByCode(dishSpec.getCode());
        if (!dishSpec.getId().equals(ds.getId())) {
            throw new ApsExeption(ResultType.RECORD_ALWAYS_EXIST);
        }

        return dishSpecRep.save(dishSpec);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @Override
    public DishSpec get(String id) {
        DishSpec ds = dishSpecRep.findOne(id);
        if (null == ds) {
            throw new ApsExeption(ResultType.RECORD_NOT_EXIST);
        }
        return ds;
    }

    /**
     * 列表查询
     *
     * @param baseQuery
     * @return
     */
    @Override
    public Pagination<DishSpec> list(BaseQuery baseQuery) {
        String sql = " select * from aps_dish_spec ads ";
        return new Pagination<DishSpec>().search(jdbcTemplate, DishSpec.class, sql, new Object[] {},
                baseQuery, "ads");
    }
}
